package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;

import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

public class SelectAllElementGenerator extends AbstractXmlElementGenerator {

	public SelectAllElementGenerator() {
		super();
	}
	
	@Override
    public void addElements(XmlElement parentElement) {
        //先创建一个select标签
         XmlElement answer = new XmlElement("select"); //$NON-NLS-1$
         //设置该select标签的id，正式第二篇里在枚举中设置的值
         answer.addAttribute(new Attribute("id", introspectedTable.getSelectAllSelectiveStatementId())); 
         if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
             answer.addAttribute(new Attribute("resultMap", introspectedTable.getResultMapWithBLOBsId()));
         } else {
        	 answer.addAttribute(new Attribute("resultMap", introspectedTable.getBaseResultMapId()));
         }

         context.getCommentGenerator().addComment(answer);
         //接下去是拼接我们的sql
         StringBuilder sb = new StringBuilder();
         sb.append("select "); 
         if (stringHasValue(introspectedTable
                 .getSelectByPrimaryKeyQueryId())) {
             sb.append('\'');
             sb.append(introspectedTable.getSelectByPrimaryKeyQueryId());
             sb.append("' as QUERYID,"); //$NON-NLS-1$
         }
         answer.addElement(new TextElement(sb.toString()));
         answer.addElement(getBaseColumnListElement());
         if (introspectedTable.hasBLOBColumns()) {
             answer.addElement(new TextElement(",")); //$NON-NLS-1$
             answer.addElement(getBlobColumnListElement());
         }

         sb.setLength(0);
         sb.append("from "); 
         sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
         answer.addElement(new TextElement(sb.toString()));

         parentElement.addElement(answer);
    }

}
